// pages/groups/room/room.js
import { getTagData, userIdParam } from "../../../utils/util";

Page({

    /**
     * 页面的初始数据
     */
    data: {
      rList: [],
      color: wx.$theme.color
    },
    tapAddRoom: function(e) {
        const {placeId} = this.data;
        wx.navigateTo({
          url: `../addRoom/addRoom?placeId=${placeId}`,
        })
    },

    tapUpdateRome: function(e) {
        const {id, rname} = getTagData(e);
        wx.navigateTo({
          url: `../addRoom/addRoom?id=${id}&rname=${rname}`,
        })
    },

    tapDeleteRome: function(e) {
        const {id, rname} = getTagData(e);
        wx.showModal({
          title: '提示',
          content: `确定删除${rname}房间？`,
          confirmColor: wx.$theme.color,
          complete: (res) => {
            if (res.confirm) {
                wx.$http.delete({url: '/szm/room/', data: [id]}).then(()=> {
                    wx.showToast({
                      title: '删除房间',
                    });
                    this.init();
                });
            }
          }
        })
    },

    bindPageList: function(e) {
      const {list, page} = e.detail;
      const {rList} = this.data;
      this.setData({rList: page === 1 ? list : rList.concat(list)});
    },

    tapSetting: function (e) {
        const {id, rname} = getTagData(e)
        wx.setStorageSync('room', {roomId: id, rname})
        wx.navigateTo({
          url: `./setting/setting?id=${id}&rname=${rname}`,
        })
    },

    init() {
        const {placeId} = this.data;
        this.page_this.data.page = 1;
        this.page_this.data.limit = 1000;
        this.page_this.getPageData({url: '/szm/room/page', data: {...userIdParam(), placeId}});
    },

    tapNvaZK: function (params) {
        const {rList} = this.data;
        wx.navigateTo({
            url: `./zk/zk`,
            success(res) {
                res.eventChannel.emit("rlist", rList);
            }
        })  
    },
    tapNvaED: function (params) {
        const {rList} = this.data;
        wx.navigateTo({
            url: `./edun/edun`,
            success(res) {
            }
        })  
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const {placeId, pname} = options;
        this.setData({
            placeId,
            rList: []
        })
        wx.setNavigationBarTitle({title : pname});
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.page_this = this.selectComponent('#page')
        this.init();  
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})